﻿using System;

using Common.Base;

using GameCommon.Manager;
using GameCommon.ManagerInterfaces;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameGraphics.Components
{
    public class BuildingProgressBar
    {
        private Texture2D texture;

        private Texture2D progressTexture;

        private Dimension progressDimension;

        private int progress;

        public BuildingProgressBar(Dimension parentDimension)
        {
            this.LoadTexture();
            this.SetDimension(parentDimension);
        }

        private void LoadTexture()
        {
            this.progressTexture = Managers.ContentManager.GetBoardTexture2D(@"Misc/LoadingBar");
            this.texture = Managers.ContentManager.GetBoardTexture2D(@"Misc/LoadingBarRaw");
        }

        public Dimension Dimension { get; private set; }

        public float Progress
        {
            set
            {
                this.progress = (int)Math.Ceiling(value * 100);
                this.progressDimension.Width = this.progress / 2;
            }
        }

        public void Draw(IDrawingManager manager)
        {
            if (this.texture != null)
            {
                manager.Add(this.texture, this.Dimension.Rectangle, Color.White, 50);
            }

            if (this.progressTexture != null)
            {
                manager.Add(this.progressTexture, this.progressDimension.Rectangle, Color.White, 51);
            }
        }

        private void SetDimension(Dimension parentDimension)
        {
            if (this.texture == null)
            {
                return;
            }

            var x = parentDimension.X + ((parentDimension.Width - this.texture.Width) / 2);
            var y = parentDimension.Y - 10;

            this.Dimension = new Dimension(x, y, new Size(this.texture.Width, this.texture.Height));
            
            this.progressDimension = new Dimension(this.Dimension.Position, 0, this.progressTexture.Height);
            this.progressDimension.Position += 1;
        }
    }
}